package org.example.topicdata;

import org.example.ConnectDatabase;
import org.example.Server;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SearchTopic extends ConnectDatabase {
    Topic result = new Topic();
    Statement sql;
    ResultSet rs;

    public String search(Topic topic){
        String topic_str ;
        System.out.println(topic.ID);
        String SQL ="select * from Topics where TID = " + topic.getID() ;
        try {
            sql = con.createStatement();
            rs = sql.executeQuery(SQL);
            if (rs.next()) {
                result.setID(rs.getInt(1));
                result.setTopicName(rs.getString(2).trim());
                result.setSelect_A(rs.getString(3).trim());
                result.setSelect_B(rs.getString(4).trim());
                result.setSelect_C(rs.getString(5).trim());
                result.setSelect_D(rs.getString(6).trim());
                result.setAns(rs.getString(7));
            }
            con.close();
            if (result.ID != 0){
                topic_str = "搜索到题目ID：第"+result.ID+"题，题目内容："+result.topicName+
                        "\n选项A:"+result.Select_A+"\t选项B:"+result.Select_B+
                        "\n选项C:"+result.Select_C+"\t选项D:"+result.Select_D+
                        "\n答案:"+result.ans;
            }else return "查无此题";

        }catch (SQLException e){
            topic_str = "查无此题";
        }
        System.out.println("返回数据");

        return topic_str;
    }
}
